package com.woniuxy.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.common.ResponseResult;
import com.woniuxy.entity.Province;
import com.woniuxy.exception.ServiceEexception;
import com.woniuxy.service.ProvinceService;

/**
 * Servlet implementation class ProvinceServlet
 */
@WebServlet("/ProvinceServlet")
public class ProvinceServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=UTF-8");
		try {
			List<Province> provinces = new ProvinceService().getAllProvinces();
			ResponseResult<List<Province>> responseResult = 
					new ResponseResult<List<Province>>(provinces,200,"sucess");
			new ObjectMapper().writeValue(resp.getWriter(), responseResult);
			
		}catch(ServiceEexception e){
			ResponseResult responseResult=
                    new ResponseResult(null, 500, e.getMessage());
            new ObjectMapper().writeValue(resp.getWriter(), responseResult);
		}
	}

}
